//
//  LPSearchContentView.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/8/21.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPSearchContentView.h"

@interface LPSearchContentView ()<UITextFieldDelegate>
/**  搜索文字  */
@property (strong, nonatomic) UILabel  *searchText;


@end
@implementation LPSearchContentView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor themeGrayColor];
        [self addSubview:self.searchTextField];
        [self addSubview:self.searchText];
        
        UIView *under = [UIView new];
        under.backgroundColor = [UIColor themeGrayColor];
        [self addSubview:under];
        under.sd_layout.leftEqualToView(self).bottomEqualToView(self).widthIs([UIScreen zj_screenWith]).heightIs(1);
    }
    return self;
}

- (void)tapClick{
    if (self.delegate) {
        [self.delegate LPSearchContentViewDelegate_clickSearch];
    }

}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.delegate) {
        [self.delegate LPSearchContentViewDelegateWithView:self string:textField.text clickReturnSeatch:NO];
    }

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    if (self.delegate) {
        [self.delegate LPSearchContentViewDelegateWithView:self string:textField.text clickReturnSeatch:YES];
    }
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (toBeString.length >= 50 && range.length!= 1 ){
        textField.text = [toBeString substringToIndex:50];
        return NO;
        
    }
    return YES;
}

- (UITextField *)searchTextField{
    if (!_searchTextField) {
        _searchTextField = ({
            UITextField *textField =  [[UITextField alloc] initWithFrame:CGRectMake(25 * [UIScreen zj_screenWScale], 7 * [UIScreen zj_screenHScale], [UIScreen zj_screenWith] - 85 * [UIScreen zj_screenWScale] , 36 * [UIScreen zj_screenHScale])];
            textField.placeholder = @"输入关键字快速查找内容";
            textField.font = [UIFont systemFontOfSize:14 * [UIScreen zj_textFontScale]];
            textField.textColor = [UIColor textBlackColor];
            textField.textAlignment = NSTextAlignmentLeft;
            UIImageView *left = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30 * [UIScreen zj_screenHScale], 15 * [UIScreen zj_screenHScale])];
            left.contentMode = UIViewContentModeCenter;
            left.image = [UIImage imageNamed:@"icon_qa_soso"];
            textField.leftViewMode = UITextFieldViewModeAlways;
            textField.leftView = left;
//            [textField setTextFieldLeftforWidth:20 * [UIScreen zj_screenWScale]];
            [textField setTextFieldRightforWidth:20 * [UIScreen zj_screenWScale]];
            textField.backgroundColor = [UIColor whiteColor];
            textField.delegate = self;
            textField.layer.cornerRadius = 8;
            textField.layer.masksToBounds = YES;
            textField.layer.borderColor = [UIColor themeGrayColor].CGColor;
            textField.layer.borderWidth = 1;
            textField.returnKeyType = UIReturnKeySearch;
            
            textField;
        });
        
    }
    return _searchTextField;
    
}



- (UILabel *)searchText{
    if (!_searchText) {
        _searchText = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen zj_screenWith] - 55 * [UIScreen zj_screenWScale], 7 * [UIScreen zj_screenHScale] + LP_StatusBarHeight - 20, 40 * [UIScreen zj_screenWScale], 36 * [UIScreen zj_screenHScale])];
            label.textAlignment = NSTextAlignmentRight;
            label.textColor = [UIColor textBlackColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = LYPFONT(16);
            label.text = @"搜索";
            label.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
            [label addGestureRecognizer:tap];
            label;
        });
    }
    return _searchText;
}


@end
